package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Role;
import com.embhealth.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    /**
     * 查询所有角色名称，用于用户页面数据回显
     * @return
     */
    @RequestMapping("/findAllRoleName")
    public Result findAllRoleName(){
        List<Map<String,Object>> nameList=roleService.findAllRoleName();
        return new Result(true,"查询全部角色项成功",nameList);
    }
    /**
     * 分页查询角色信息
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }
    /**
     * 新增用户信息，需要传递三个参数
     * @param urls
     * @param permissions
     * @param role
     * @return
     */
    @RequestMapping("/add")
    public Result addRole(Integer[] urls,Integer[] permissions,@RequestBody Role role){
        try {
            roleService.addRole(role,permissions,urls);
            return new Result(true,"添加用户成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }
    /**
     * 根据role_id查询基础的角色信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true,"查询角色成功",role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询角色失败");
        }
    }
    /**
     * 根据role_id查询关联的执行权限
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionsByRoleId")
    public Result findPermissionsByRoleId(Integer id){
        try {
            Integer[] permissions=roleService.findPermissionsByRoleId(id);
            return new Result(true,"查询关联执行权限成功",permissions);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联执行权限失败");
        }
    }
    /**
     * 根据role_id；查询关联的菜单权限
     * @param id
     * @return
     */
    @RequestMapping("/findUrlsByRoleId")
    public Result findUrlsByRoleId(Integer id){
        try {
            Integer[] urls=roleService.findUrlsByRoleId(id);
            return new Result(true,"查询关联执行权限成功",urls);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询关联执行权限失败");
        }
    }

    /**
     * 更新角色信息
     * @param urls
     * @param permissions
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(Integer[] urls,Integer[] permissions,@RequestBody Role role){
        try{
            roleService.edit(urls,permissions,role);
            return new Result(true,"修改角色数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改角色数据失败");
        }
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            roleService.delete(id);
            return new Result(true,"删除角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除角色失败");
        }
    }
}
